package hn.cch.retry.component;

import hn.cch.retry.service.RetryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;

@Component
public class RetryComponent {

    private final static Logger logger = LoggerFactory.getLogger(RetryComponent.class);

    @Autowired
    private RetryService retryService;

    public void runRetry(){
        // testRetry();
        retryService.retryMethod();
    }


    @Retryable(
        include = {ArithmeticException.class},
        exclude = {NullPointerException.class},
        maxAttempts = 3,
        backoff = @Backoff(delay = 3 * 1000L, multiplier = 2)
    )
    public void testRetry() {
        logger.info("retry start");
        double a = 1 / 0;
        logger.info("retry end");
    }

    @Recover
    public void testRetryRecover(Throwable throwable) {
        logger.error("test retry recover", throwable);
    }

}
